package com.ccrfid.rmc6.resource.alarm;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.alarm.AlarmNotifyStrategyEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.alarm.AlarmNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("alarm-notify-strategies")
@Produces(MediaType.APPLICATION_JSON)
public class AlarmNotifyStrategyResource {
    @Autowired
    private AlarmNotifyService alarmNotifyService;

    @GET
    public Page<AlarmNotifyStrategyEntity> getAlarmNotifyStrategyPage(@QueryParam("strategyName") String strategyName,
                                                                      @QueryParam("enabled") Integer enabled,
                                                                      @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (strategyName != null) {
            criteria.add(new Criterion("strategyName", Criterion.Operator.LIKE, strategyName + '%'));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return alarmNotifyService.getAlarmNotifyStrategyPage(criteria, pageRequest);
    }

    @GET
    @Path("{strategyId}")
    public AlarmNotifyStrategyEntity getAlarmNotifyStrategyById(@PathParam("strategyId") Long strategyId) {
        return alarmNotifyService.getAlarmNotifyStrategyById(strategyId);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public AlarmNotifyStrategyEntity createAlarmNotifyStrategy(AlarmNotifyStrategyEntity entity) throws AppException {
        return alarmNotifyService.createAlarmNotifyStrategy(entity);
    }

    @PUT
    @Path("{strategyId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public AlarmNotifyStrategyEntity updateAlarmNotifyStrategy(@PathParam("strategyId") Long strategyId,
                                                               AlarmNotifyStrategyEntity entity) throws AppException {
        return alarmNotifyService.updateAlarmNotifyStrategy(strategyId, entity);
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean deleteAlarmNotifyStrategies(List<Long> idList) throws AppException {
        return alarmNotifyService.deleteAlarmNotifyStrategies(idList);
    }
}
